<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <!-- 新增角色按钮 -->
        <el-row style="height:60px">
          <el-button
            icon="el-icon-plus"
            size="small"
            type="primary"
            @click="updateVisible"
          >新增角色</el-button>
        </el-row>
        <!-- 表格 -->
        <el-table :data="roleList">
          <el-table-column label="序号" width="120" type="index" :index="evenNumber" />
          <el-table-column label="角色名称" width="240" prop="name" />
          <el-table-column label="描述" prop="description" />
          <el-table-column label="操作" width="240" fixed="right">
            <template #default="{row}">
              <el-button size="small" @click="assignPermission(row.id)">分配权限</el-button>
              <el-button size="small" @click="editRole(row.id)">编辑</el-button>
              <el-button size="small" type="danger" @click="delRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <!-- 分页组件 -->
          <el-pagination layout="prev,pager,next" :total="total" @current-change="changePage" />
        </el-row>
      </el-card>
      <add-role ref="addRole" :dialog-visible="dialogVisible" @update-list="getRoleList" @closeDialog="closeDia" />
    </div>
    <el-dialog

      title="分配权限(一级为路由页面查看权限-二级为按钮操作权限)"
      :visible="showAssignDialog"
      @close="closeAssignDialog"
      @open="openAssignDialog"
    >
      <!-- 权限点数据展示 -->
      <el-tree
        ref="tree"
        :data="assignPermissionList"
        :props="{ label: 'name' }"
        :default-expand-all="true"
        :show-checkbox="true"
        :check-strictly="true"
        node-key="id"
      />
      <template #footer>
        <div style="text-align: right;">
          <el-button @click="closeAssignDialog">取消</el-button>
          <el-button type="primary" @click="confirm">确定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
<script>
import { fetchSettingsAPI, delRoleInfoAPI } from '@/api/settings'
import addRole from '@/views/Setting/components/addRole.vue'
import { transTree } from '@/utils/transTree'
export default {
  components: {
    addRole
  },
  data() {
    return {
      showAssignDialog: false,
      assignPermissionList: [],
      curId: '',
      dialogVisible: false,
      roleList: [],
      total: 0,
      params: {
        page: 1,
        pagesize: 10
      }
    }
  },
  mounted() {
    this.getRoleList()
  },
  methods: {

    async getRoleList() {
      const res = await fetchSettingsAPI(this.params)
      this.roleList = res.rows
      this.total = res.total
    },
    changePage(page) {
      this.params.page = page
      this.evenNumber()
      this.getRoleList()
    },
    delRole(id) {
      this.$confirm('确定删除此角色吗, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await delRoleInfoAPI(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.getRoleList()
      }).catch(() => {

      })
    },
    updateVisible() {
      this.dialogVisible = true
    },
    closeDia() {
      this.dialogVisible = false
    },
    editRole(id) {
      this.$refs.addRole.roleInfoBackFill(id)
      this.dialogVisible = true
    },
    evenNumber(index) {
      return index + (this.params.page - 1) * this.params.pagesize + 1
    },
    assignPermission(id) {
      this.curId = id
      this.showAssignDialog = true
    },
    closeAssignDialog() {
      this.showAssignDialog = false
    },
    async openAssignDialog() {
      const res = await this.$http.get('/sys/permission')
      this.assignPermissionList = transTree(res)
      const detail = await this.$http.get(`/sys/role/${this.curId}`)
      // console.log(detail)
      this.$refs.tree.setCheckedKeys(detail.permIds)
    },
    async confirm() {
      const res = this.$refs.tree.getCheckedKeys()
      await this.$http.put('/sys/role/assignPrem', {
        id: this.curId,
        permIds: res
      })
      this.$message.success('分配权限成功')
      this.showAssignDialog = false
    }
  }
}
</script>
